package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IFileContractService;
import com.mdd.admin.validate.FileContractCreateValidate;
import com.mdd.admin.validate.FileContractUpdateValidate;
import com.mdd.admin.validate.FileContractSearchValidate;
import com.mdd.admin.vo.FileContractListedVo;
import com.mdd.admin.vo.FileContractDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.FileContract;
import com.mdd.common.mapper.FileContractMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 合同文件实现类
 * @author LikeAdmin
 */
@Service
public class FileContractServiceImpl implements IFileContractService {
        
    @Resource
    FileContractMapper fileContractMapper;

    /**
     * 合同文件列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FileContractListedVo>
     */
    @Override
    public PageResult<FileContractListedVo> list(PageValidate pageValidate, FileContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<FileContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        fileContractMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:contractId@contract_id:str",
            "like:contractName@contract_name:str",
            "like:customerName@customer_name:str",
            "=:fileDescription@file_description:str",
            "like:fileName@file_name:str",
            "=:fieldStaff@field_staff:str",
            "lte:creationTime@creation_time:str",
            "=:uploader:str",
            "lte:uploadTime@upload_time:str",
        });

        IPage<FileContract> iPage = fileContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<FileContractListedVo> list = new LinkedList<>();
        for(FileContract item : iPage.getRecords()) {
            FileContractListedVo vo = new FileContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 合同文件详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return FileContract
     */
    @Override
    public FileContractDetailVo detail(Integer id) {
        FileContract model = fileContractMapper.selectOne(
                new QueryWrapper<FileContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        FileContractDetailVo vo = new FileContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 合同文件新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(FileContractCreateValidate createValidate) {
        FileContract model = new FileContract();
        model.setUpdateUserId(createValidate.getUpdateUserId());
        model.setUpdateUserName(createValidate.getUpdateUserName());
        model.setContractId(createValidate.getContractId());
        model.setContractName(createValidate.getContractName());
        model.setCustomerName(createValidate.getCustomerName());
        model.setFileDescription(createValidate.getFileDescription());
        model.setFileDescriptionid(createValidate.getFileDescriptionid());
        model.setFileName(createValidate.getFileName());
        model.setFileType(createValidate.getFileType());
        model.setFieldStaff(createValidate.getFieldStaff());
        model.setFieldStaffId(createValidate.getFieldStaffId());
        model.setCreationTime(createValidate.getCreationTime());
        model.setUploader(createValidate.getUploader());
        model.setUploadTime(createValidate.getUploadTime());
        fileContractMapper.insert(model);
    }

    /**
     * 合同文件编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(FileContractUpdateValidate updateValidate) {
        FileContract model = fileContractMapper.selectOne(
                new QueryWrapper<FileContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setId(updateValidate.getId());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        model.setContractId(updateValidate.getContractId());
        model.setContractName(updateValidate.getContractName());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setFileDescription(updateValidate.getFileDescription());
        model.setFileDescriptionid(updateValidate.getFileDescriptionid());
        model.setFileName(updateValidate.getFileName());
        model.setFileType(updateValidate.getFileType());
        model.setFieldStaff(updateValidate.getFieldStaff());
        model.setFieldStaffId(updateValidate.getFieldStaffId());
        model.setCreationTime(updateValidate.getCreationTime());
        model.setUploader(updateValidate.getUploader());
        model.setUploadTime(updateValidate.getUploadTime());
        fileContractMapper.updateById(model);
    }

    /**
     * 合同文件删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        FileContract model = fileContractMapper.selectOne(
                new QueryWrapper<FileContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        fileContractMapper.updateById(model);
    }

}
